OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//销售订单审核确认
OB.Emes_ActionButton.SaleOrderAudit = function(params, view) {
    var viewGrid = view.activeView.viewGrid;
    var records = viewGrid.getSelectedRecords();
    var ids = [];
    for ( var i = 0; i < records.length; i++) {
        ids.push(records[i].id);
    }
    if (ids.length > 0) {
        isc.confirm("确定将选中的 " + ids.length + " 条订单确认通过？", function(bool) {
            if (bool) {
                var data = {
                    ids: ids.join(",")
                };
                var callback = function(response, data, request) {
                    isc.showMessage(data.msg);
                    if(data.result) {
                        view.refresh();
                    }
                };
                OB.RemoteCallManager.call("com.mes.handler.plan.SaleOrderAuditHandler", data, {}, callback);
            }
        });
    } else {
        isc.showMessage("请选择记录！");
    }
};
//计划订单下发
OB.Emes_ActionButton.PlanOrderIssued = function(params, view) {
    var viewGrid = view.activeView.viewGrid;
    var records = viewGrid.getSelectedRecords();
    var ids = [];
    for ( var i = 0; i < records.length; i++) {
        ids.push(records[i].id);
    }
    if (ids.length > 0) {
		isc.confirm("确定将选中的 " + ids.length + " 条订单下发到生产计划？", function(bool) {
			if (bool) {
				var data = {
					ids: ids.join(",")
				};
				var callback = function(response, data, request) {
					isc.showMessage(data.msg);
					if(data.result) {
						view.refresh();
					}
				};
				OB.RemoteCallManager.call("com.mes.handler.plan.PlanOrderIssuedHandler", data, {}, callback);
			}
		});
	} else {
		isc.showMessage("请选择记录！");
	}
};

var planOrderView = null;
//计划订单分解下发
OB.Emes_ActionButton.planOrderSplitIssued = function(params, view) {
	var grid = view.activeView.viewGrid;
	var record = grid.getSelectedRecord();

	if (record) {
		isc.confirm("确定将订单下发到生产计划？", function(bool) {
			if (bool) {
                planOrderView = view;
				isc.Emes_PlanOrderSplitIssuedWindow.create(record[OB.Constants.ID]).show();
			}
		});
	}
};

//计划订单审核
OB.Emes_ActionButton.PlanOrderAudit = function(params, view) {
    var viewGrid = view.activeView.viewGrid;
    var record = viewGrid.getSelectedRecord();
    if (record) {
        isc.confirm("确定将订单审核通过？", function(bool){
            if (bool) {
                var data = {
                    recordId: record[OB.Constants.ID]
                };
                var callback = function(response, data, request){
                    isc.showMessage(data.msg);
                    if(data.result){
                        view.activeView.refresh();
                    }
                };
                OB.RemoteCallManager.call("com.mes.handler.plan.PlanOrderAuditHandler", data, {}, callback);
            }
        });
    } else {
        isc.showMessage("请选择记录！");
    }
};
//计划订单导入
OB.Emes_ActionButton.Import_PlanOrder_Btn = function(params, view) {
    var viewGrid = view.activeView.viewGrid;
    var record = viewGrid.getSelectedRecord();
	OB.MyPlanOrderRefresh = function(){
		view.activeView.refresh();
	}
	
	OB.Layout.ClassicOBCompatibility.Popup.open('ImportPlanOrderData', 500, 100,OB.Application.contextUrl + 'web/com.mes/html/ImportPlanOrderData.html?DataId='+record.id,
				'计划订单导入', window, false, false, null, true, true);
    OB.Emes_ActionButton.MyRefresh=function(){
        view.activeView.refresh();
    }
};

isc.defineClass("Emes_PlanOrderSplitIssuedWindow", isc.OBPopup).addProperties({
    width: 320,
    height: 130,
    title: '调拨申请单审核',
    showMinimizeButton: false,
    showMaximizeButton: false,
    initWidget: function (id) {
        //弹出自定义窗口
        this.msgInfo = isc.Label.create({
            align: "center",
            padding: 5,
            width: "100%",
            contents: "<p align='left' style='padding-left:50px;'>分解每个订单数量</p><p><input id='splitQty' /></p>"
        });

        var _self = this;
        var callback = function (response, data, request) {
            _self.closeClick();
            isc.showMessage(data.msg);
            planOrderView.refresh();
        };
        var okBtn = isc.OBFormButton.create({
            title: '确定',
            popup: this,
            action: function () {
                var value = document.getElementById("splitQty").value;
                if (!value || !/^([1-9]+[0-9]{0,})$/.test(value)) {
                    isc.showMessage("数量只能为正整数！");
                    return;
                }
                var data = {
                    id: id,
                    qty: value * 1
                };
                OB.RemoteCallManager.call("com.mes.handler.plan.PlanOrderSplitIssuedHandler", data, {}, callback);
            }
        });
        var cancelBtn = isc.OBFormButton.create({
            title: '取消',
            popup: this,
            action: function () {
                _self.closeClick();
            }
        });

        this.items = [
            isc.VLayout.create({
                width: 320,
                height: 130,
                members: [
                    this.msgInfo,
                    isc.HLayout.create({
                        height: 30,
                        align: "center",
                        padding: 5,
                        members: [
                            okBtn,
                            cancelBtn
                        ]
                    })
                ]
            })
        ];
        this.Super("initWidget", arguments);
    }
});